package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.lxj.xpopup.core.BottomPopupView
import com.netease.nim.uikit.R
import com.netease.nim.uikit.api.SessionHelper
import com.netease.nim.uikit.business.uinfo.UserInfoHelper
import com.netease.nim.uikit.common.ui.imageview.HeadImageView
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import com.netease.nimlib.sdk.team.model.TeamMember
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.observeObject
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.helper.loading.KeyHelper


/**
 * @author beni
 * @Description: 用户详情的底部弹窗
 * @date 2021/9/9 15:19
 */
class BottomUserDetailPopupView(
    context: Context,
    private val isMyFriend: Boolean,
    private val onlineStatus: Boolean,
    private val userInfo: TeamMember,
    private val resultCallBack: ResultUiCallBack<TeamMember>,
) : BottomPopupView(context), View.OnClickListener {

    private lateinit var imgAvatar: HeadImageView
    private lateinit var imgOnlineStatus: ImageView
    private lateinit var imgMore: ImageView
    private lateinit var tvName: TextView
    private lateinit var llOne: View
    private lateinit var llTwo: View
    private lateinit var llThree: View
    private lateinit var llFour: View
    private lateinit var imgOne: ImageView
    private lateinit var imgTwo: ImageView
    private lateinit var imgThree: ImageView
    private lateinit var imgFour: ImageView
    private lateinit var tvOne: TextView
    private lateinit var tvTwo: TextView
    private lateinit var tvThree: TextView
    private lateinit var tvFour: TextView

    override fun getImplLayoutId(): Int = R.layout.popupview_user_detail

    override fun onCreate() {
        super.onCreate()
        findView()

        if (isMyFriend) {
            tvOne.text = "个人主页"
            tvTwo.text = "提及"
            tvThree.text = "私信"
            imgOne.setImageResource(R.drawable.ic_circle_home)
            imgTwo.setImageResource(R.drawable.ic_circle_mention)
            imgThree.setImageResource(R.drawable.ic_circle_private_letter)
            llFour.visibility = INVISIBLE
        } else {
            tvOne.text = "个人主页"
            tvTwo.text = "提及"
            tvThree.text = "添加"
            imgOne.setImageResource(R.drawable.ic_circle_home)
            imgTwo.setImageResource(R.drawable.ic_circle_mention)
            imgThree.setImageResource(R.drawable.ic_circle_addition)
            llFour.visibility = INVISIBLE
        }

        imgAvatar.loadBuddyAvatar(userInfo.account)
        tvName.text = UserInfoHelper.getUserDisplayNameInSession(
            userInfo.account,
            SessionTypeEnum.Team,
            SessionHelper.currentChannelVO?.teamId
        )

        imgOnlineStatus.setImageResource(
            if (onlineStatus)
                R.drawable.ic_circle_green_middle_1 else
                R.drawable.ic_circle_gray_middle_1
        )
        setOnClickListenerAgency(this, llOne, llTwo, llThree, llFour, imgMore)
    }

    private fun findView() {
        imgAvatar = findViewById(R.id.imgAvatar)
        imgOnlineStatus = findViewById(R.id.imgOnlineStatus)
        imgMore = findViewById(R.id.imgMore)
        tvName = findViewById(R.id.tvName)
        llOne = findViewById(R.id.llOne)
        llTwo = findViewById(R.id.llTwo)
        llThree = findViewById(R.id.llThree)
        llFour = findViewById(R.id.llFour)
        imgOne = findViewById(R.id.imgOne)
        imgTwo = findViewById(R.id.imgTwo)
        imgThree = findViewById(R.id.imgThree)
        imgFour = findViewById(R.id.imgFour)
        tvOne = findViewById(R.id.tvOne)
        tvTwo = findViewById(R.id.tvTwo)
        tvThree = findViewById(R.id.tvThree)
        tvFour = findViewById(R.id.tvFour)
    }

    override fun onClick(v: View) {
        resultCallBack.invoke(v, userInfo, this)
    }

}